package com.gaoyi.crm.adapter;

import android.content.Context;

import com.gaoyi.crm.BR;
import com.gaoyi.crm.R;
import com.gaoyi.crm.adapter.base.BaseDtoAdapter;
import com.gaoyi.crm.adapter.base.BaseViewHolder;
import com.gaoyi.crm.databinding.RvRecordBinding;
import com.gaoyi.crm.db.dto.PmnDto;

public class RecordAdapter extends BaseDtoAdapter<RvRecordBinding, PmnDto> {

    private Integer mLastPosition;

    public Integer getCheckPosition() {
        return mLastPosition;
    }

    public void setCheckPosition(Integer mLastPosition) {
        this.mLastPosition = mLastPosition;
    }

    public RecordAdapter(Context context) {
        super(context);
    }

    @Override
    protected void setBindingVariable(RvRecordBinding binding) {
        binding.setVariable(BR.format, "yyyy/MM/dd");
    }

    @Override
    protected void onBindItem(RvRecordBinding binding, PmnDto item, int position) {
        binding.llRecord.setSelected(item.isCheck());
    }

    @Override
    protected void onCreateView(RvRecordBinding binding, BaseViewHolder holder) {

    }

    @Override
    protected int getLayoutResId() {
        return R.layout.rv_record;
    }

    public void changeSelected(int position) {
        boolean check = getCurrentList().get(position).isCheck();
        if (mLastPosition == null) {
            mLastPosition = position;
        } else {
            if (mLastPosition == position) {
                if (check) {
                    mLastPosition = null;
                }
            } else {
                getCurrentList().get(mLastPosition).setCheck(false);
                notifyItemChanged(mLastPosition);
                mLastPosition = position;
            }
        }
//        if (mLastPosition != null && mLastPosition != position) {
//            getCurrentList().get(mLastPosition).setCheck(false);
//            notifyItemChanged(mLastPosition);
//        }
//        mLastPosition = position;
        getCurrentList().get(position).setCheck(!check);
        notifyItemChanged(position);
    }
}
